<template>
	<view class="login-container">
		<view class="login-box">
			<text class="login-title">登录</text>
			<view class="input-item">
				<input type="text" placeholder="请输入用户名" v-model="username" />
				<text class="error-tip" v-if="showUsernameError">请输入用户名</text>
			</view>
			<view class="input-item">
				<input password="true" placeholder="请输入密码" v-model="password" />
				<text class="error-tip" v-if="showPasswordError">请输入密码</text>
			</view>
			<button class="login-btn" @click="handleLogin">登录</button>
			<view class="register-tip">
				没有账号？<text class="register-link" @click="toRegister">去注册</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "Login",
		data() {
			return {
				username: "",
				password: "",
				showUsernameError: false,
				showPasswordError: false
			}
		},
		methods: {
			handleLogin() {
				// 重置错误提示状态
				this.showUsernameError = false;
				this.showPasswordError = false;

				// 表单校验
				let isValid = true;
				// trim()是删除字符串两端的空白字符
				if (!this.username.trim()) {
					this.showUsernameError = true;
					isValid = false;
				}

				if (!this.password.trim()) {
					this.showPasswordError = true;
					isValid = false;
				}

				// 如果校验通过则跳转
				if (isValid) {
					uni.switchTab({
						url: '/pages/tabBar/My/My'
					})

					uni.setStorageSync('username', this.username)
					// uni.setStorageSync('密码', this.password)

					uni.showToast({
						title: "登录成功"
					})
					
					uni.setTabBarItem({
					  index: 3,
					  text: '我的',
					   
					})
				}
			},

			toRegister() {
				// 跳转到注册页面
				uni.redirectTo({
					url: "/pages/LoginPage/Register",
				});
			}
		},
	};
</script>

<style scoped>
	.login-container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #f5f5f5;
	}


	.login-box {
		width: 80%;
		max-width: 400px;
		background-color: #fff;
		padding: 24px;
		border-radius: 8px;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
	}

	.login-title {
		display: block;
		text-align: center;
		font-size: 20px;
		font-weight: bold;
		margin-bottom: 24px;
	}

	.input-item {
		margin-bottom: 32rpx;
		height: auto;
	}

	.input-item input {
		width: 100%;
		padding-left: 10rpx;
		border: 1px solid #ddd;
		border-radius: 4px;
		box-sizing: border-box;
		height: 80rpx;
		line-height: 80rpx;
	}

	/* 错误提示样式 */
	.error-tip {
		display: block;
		color: #ff3b30;
		font-size: 12px;
		margin-top: 8rpx;
		line-height: 1.4;
	}

	.login-btn {
		width: 100%;
		background-color: #007aff;
		color: #FFFFFF;
	}

	.register-tip {
		text-align: center;
		margin-top: 16px;
		font-size: 14px;
		color: #666;
	}

	.register-link {
		color: #007aff;
	}
</style>